// pages/login/index.js
wx.cloud.init({
  traceUser: true
})
const db = wx.cloud.database()
const user = db.collection('user')
const app = getApp()
import { dateFormat } from '../../utils/dateFormat.js'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    userName: '',
    loadModal: true
  },

  login(e) {
    if(this.data.userName.trim() == '') {
      return
    }
    this.setData({
      loadModal: true
    })
      if (app.userDataId && app.userDataId != ''){
        user.doc(app.userDataId).update({
          data: {
            username: this.data.userName
          },
          success: res => {
            app.user.username = this.data.userName
            wx.cloud.callFunction({
              name: 'registerSuccess',
              data: {
                id: app.userDataId,
                formId: e.detail.formId,
                username: this.data.userName,
                createTime: dateFormat(new Date(), 'yyyy-MM-dd hh:mm:ss')
              }
            })
            this.setData({
              loadModal: false
            })
          }
        })
        wx.redirectTo({
          url: '/pages/index/index',
        })
      }else{
        app.updateNameCallback = userDataId => {
        if (userDataId != '') {
          user.doc(userDataId).update({
            data: {
              username: this.data.userName
            },
            success: res => {
              app.user.username = this.data.userName
              wx.cloud.callFunction({
                name: 'registerSuccess',
                data: {
                  id: userDataId,
                  formId: e.detail.formId,
                  username: this.data.userName,
                  createTime: dateFormat(new Date(), 'yyyy-MM-dd hh:mm:ss')
                }
              })
              this.setData({
                loadModal: false
              })
            }
          })
          wx.redirectTo({
            url: '/pages/index/index',
          })
        }
      }
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if (app.userDataId && app.userDataId != '') {
      user.doc(app.userDataId).get({
        success: res => {
          if (res.data.username != '') {
            app.user = res.data
            wx.redirectTo({
              url: '/pages/index/index',
            })
          } else {
            app.user = res.data
            this.setData({
              loadModal: false
            })
          }
        }
      })
    }else{
      app.userDataIdCallback = userDataId => {
        if (userDataId != ''){
          user.doc(userDataId).get({
            success: res => {
              if (res.data.username != '') {
                app.user = res.data
                wx.redirectTo({
                  url: '/pages/index/index',
                })
              } else {
                app.user = res.data
                this.setData({
                  loadModal: false
                })
              }
            }
          })
        }
      }
    }
    
  },

  getUsername(e){
    this.setData({
      userName: e.detail.value
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  // onShareAppMessage: function () {

  // }
})